import com.sun.corba.se.spi.orbutil.fsm.Action;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator implements ActionListener {

    private final JFrame frame = new JFrame("Calculator");
    private final JTextField textField=new JTextField();
    private final JPanel  panel=new JPanel();
    private final JButton[]  buttons = new JButton[24];

    private final String[] bintitles={
            "%","sqrt","x2","1/x",
            "ce","c","back","/",
            "7","8","9","X",
            "4","5","6","-",
            "1","2","3","+",
            "+-","0",".","="
    };


    public Calculator(){

        frame.setSize(450,550);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);

        textField.setBounds(0,30,430,60);           //文本框大小
        textField.setVisible(true);                                    //设置是否显示
        textField.setFont(new Font("Arial",Font.PLAIN,25)); //设置文本框内的字体字号
        textField.setHorizontalAlignment(SwingConstants.RIGHT);        //设置文本框内文字右对齐
        textField.setText("0");                                        //第一次加载文本框为0
        textField.setFocusable(false);                                 //设置文本框不能被操控
        frame.add(textField);


        panel.setBounds(10,100,420,400);
        panel.setVisible(true);
        frame.add(panel);
        panel.setBackground(Color.GRAY);
        panel.setLayout(new GridLayout(6,4,10,10));

        for(int i=0;i<24;i++){

            buttons[i]=new JButton();
            buttons[i].setVisible(true);
            buttons[i].setText(bintitles[i]);
            buttons[i].setFont(new Font("Arial",Font.PLAIN,25));
            buttons[i].setActionCommand(bintitles[i]);
            buttons[i].addActionListener(this);
            panel.add(buttons[i]);
        }



        frame.setVisible(true);



    }




    public static void main(String[] args){

        new Calculator();


    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String command=e.getActionCommand();
        System.out.println(command);
        double he=0;
        double n1;
        switch (command){

            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                String zz=textField.getText();
                String zfc=zz.substring(0,1);
                System.out.println("awd"+zfc);
                if(zfc.equals("0")){
                    textField.setText("");

                }
                textField.setText(textField.getText()+command);
                break;
            case ".":
                if(!textField.getText().contains(".")){
                    textField.setText(textField.getText()+".");
                    break;
                }else{
                    break;
                }

            case "c":
                textField.setText("0");
                break;
            case "ce":
                textField.setText("0");
                break;
            case "back":
                String st=textField.getText();

                if(!st.isEmpty()){
                    String zf=st.substring(0,st.length()-1);
                    textField.setText(zf.isEmpty()?"0":zf);
                }
                break;
            case "+-":
                n1=Double.parseDouble(textField.getText());
                n1*=-1;
                textField.setText(String.valueOf(n1));
                break;
            case "sqrt":
                n1=Double.parseDouble(textField.getText());
                n1=Math.sqrt(n1);
                textField.setText(String.valueOf(n1));
                break;




        }

    }
}
